package com.service.impl;

import com.dao.ResourcesDao;
import com.dao.impl.ResourcesDaoImpl;
import com.pojo.Button;
import com.pojo.Resources;
import com.service.ResourcesService;
import com.util.IndexJson;
import com.util.IninJson;
import com.util.LayuiTable;
import com.vo.Menus;
import com.vo.ResourcesDtree;

import java.util.ArrayList;
import java.util.List;

public class ResourcesServiceImpl implements ResourcesService {
    ResourcesDao resourcesDao = new ResourcesDaoImpl();

    @Override
    public List<Resources> findAll() {
        return resourcesDao.getResourcesAll();
    }

    @Override
    public List<Resources> findAll(int uid) {
        return resourcesDao.getResourcesAll(uid);
    }

    @Override
    public List<Resources> findAll(int uid, int mid) {
        return resourcesDao.getResourcesBtnAll(uid,mid);
    }

    @Override
    public List<Resources> findAllResources(int mtype) {
        mtype=mtype-2;
        return resourcesDao.getMenuByType(mtype);
    }

    @Override
    public int addServiceResources(Resources resources) {
        return resourcesDao.addResources(resources);
    }

    @Override
    public IninJson findMenuAll(int uid) {
        //用来存储对应二级菜单的集合
        List<IndexJson> list1 = new ArrayList<IndexJson>();
        //根据用户菜单查询1级目录
        List<Resources> resourcesAll1 = resourcesDao.getUserMenu(uid,0);
        for (Resources resources1:resourcesAll1) {
            IndexJson indexJson1 = new IndexJson();
            indexJson1.setTitle(resources1.getResName());
            indexJson1.setTarget("_self");
            indexJson1.setHref("");
            indexJson1.setIcon(resources1.getIcon());
            //用来存储对应二级菜单的集合
            List<IndexJson> list2 = new ArrayList<IndexJson>();
            //根据用户菜单查询菜单
            List<Resources> resourcesAll2 = resourcesDao.getUserMenu(uid,1);
            for (Resources resources2:resourcesAll2) {
                if(resources1.getId()==resources2.getParendId()){
                    IndexJson IndexJson2 = new IndexJson();
                    IndexJson2.setTitle(resources2.getResName());
                    IndexJson2.setTarget("_self");
                    IndexJson2.setHref(resources2.getResUrl()+"&mid="+resources2.getId());
                    IndexJson2.setIcon(resources2.getIcon());
                    list2.add(IndexJson2);
                }
            }
            indexJson1.setChild(list2);
            list1.add(indexJson1);
        }

        IninJson ininJson = new IninJson();
        ininJson.setHomeInfo(new IndexJson("首页","page/welcome-1.html?t=1"));
        ininJson.setLogoInfo(new IndexJson("LAYUI MINI","images/logo.png",""));

        IndexJson Minfo=new IndexJson("权限管理","fa fa-address-book","","_self",list1);
        List<IndexJson> menuInfo = new ArrayList<IndexJson>();
        menuInfo.add(Minfo);

        ininJson.setMenuInfo(menuInfo);
        return ininJson;
    }

    @Override
    public Resources findResourcesById(int mid) {
        return resourcesDao.getMenuById(mid);
    }

    @Override
    public List<Button> getFindButtonAll() {
        return resourcesDao.getButtonAll();
    }

    @Override
    public LayuiTable<Menus> allResouce() {
        LayuiTable<Menus>  layui = new LayuiTable<Menus>();
        layui.setCode(0);
        layui.setMsg("");
        layui.setCount(0);
        List<Resources> resourcesAll = resourcesDao.getResourcesAll();
        List<Menus> list = new ArrayList<Menus>();
        for (Resources resources:resourcesAll) {
            Menus menuAll = new Menus();
            menuAll.setAuthorityId(resources.getId());
            menuAll.setAuthorityName(resources.getResName());
            menuAll.setAuthority(resources.getResKey());
            menuAll.setChecked(true);
            menuAll.setOpen(true);
            menuAll.setIsMenu(resources.getType());
            if(resources.getType()==0){
                menuAll.setParentId(resources.getParendId()-1);
            }else {
                menuAll.setParentId(resources.getParendId());
            }
            list.add(menuAll);
        }
        layui.setData(list);
        return layui;
    }

    @Override
    public LayuiTable<ResourcesDtree> getMenuDtree() {
        LayuiTable<ResourcesDtree>  layui = new LayuiTable<ResourcesDtree>();
        layui.setCode(0);
        layui.setMsg("");
        layui.setCount(1000);


        List<ResourcesDtree> dtreeList = new ArrayList<ResourcesDtree>();

        List<Resources> resourcesAll = resourcesDao.getResourcesAll();
        for (Resources resources:resourcesAll){
            ResourcesDtree dtree = new ResourcesDtree();
            dtree.setId(resources.getId());
            dtree.setCheckArr("0");
            dtree.setTitle(resources.getResName());
            dtree.setParentId(resources.getParendId());
            dtreeList.add(dtree);
        }

        layui.setData(dtreeList);
        return layui;
    }


}
